//  [9/18/2013 OMEGA] created

#include <QtWidgets/QDialog>
#include "DuelAssECommon.h"
#include "DuelAssENoFocusDelegate.h"
#include "DuelAssEImportResourceDlg.h"

DAssEImportResourceDlg::DAssEImportResourceDlg( QWidget* parent /*= 0*/ ) : 
	QDialog(parent),
	mItemDelegate(this)
{
	ui.setupUi(this);
	mModel.setFilter(QDir::Files | QDir::NoDotAndDotDot);
	mModel.setNameFilterDisables(false);
	// set the item delegate to your table widget
	ui.treeView->setItemDelegate(&mItemDelegate);
	ui.treeView->setSelectionMode(QAbstractItemView::MultiSelection);
	ui.treeView->setSelectionBehavior(QAbstractItemView::SelectRows);
	ui.treeView->setFocusPolicy(Qt::ClickFocus);
	ui.treeView->setItemsExpandable(false);
	ui.treeView->setRootIsDecorated(false);
	ui.treeView->setModel(&mModel);
}

void DAssEImportResourceDlg::setOpenFolder( const QString& path, const QString& archName )
{
	mPath = path;
	mDestArchiveName = archName;
	ui.label->setText(tr("Choose files you want to import from the following list, \
		which are located in :\n").append(mDestArchiveName));
}

void DAssEImportResourceDlg::setExludeFiles( const QStringList& list )
{
	mExcludeFiles = list;
}

int DAssEImportResourceDlg::exec()
{
	QDir dir(mPath);
	QStringList entries = dir.entryList(QStringList());
	for (int i = 0; i < entries.size(); ++i)
	{
		for (int j = 0; j < mExcludeFiles.size(); ++j)
		{
			if (entries.at(i) == mExcludeFiles.at(j))
			{
				entries.takeAt(i);
				mExcludeFiles.takeAt(j);
			}
		}
	}
	mModel.setNameFilters(entries);
	ui.treeView->setRootIndex(mModel.setRootPath(mPath));

	//mModel.se
	return QDialog::exec();
}

QStringList DAssEImportResourceDlg::getSelectedFiles()
{
	QStringList ret;
	QModelIndexList idxList = ui.treeView->selectionModel()->selectedRows(0);
	foreach(QModelIndex idx, idxList)
	{
		ret.push_back(mModel.data(idx).toString());
	}
	return ret;
}
